from django.db import models
import uuid
from control.models.User import User


class BasicDimension(models.Model):
    uuid = models.UUIDField(primary_key=True, auto_created=True,
                            default=uuid.uuid4, editable=False, verbose_name="uuid")
    basic_dimension_name = models.CharField(
        max_length=200, verbose_name="基本量纲名称", null=False, blank=True, default="")
    basic_dimension_name_en = models.CharField(
        max_length=200, verbose_name="基本量纲英文名称", null=False, blank=True, default="")
    sei_id = models.CharField(
        max_length=200, verbose_name="基本量纲编码", null=False, blank=True, default="")
    basic_dimension_symbol = models.CharField(
        max_length=256, null=False, blank=False, verbose_name="基本量纲符号", default="")
    basic_dimension_symbol_ascii = models.CharField(
        max_length=256, null=False, blank=True, default="", verbose_name="基本量纲ascii符号")
    basic_dimension_symbol_html = models.CharField(
        max_length=256, null=False, blank=True, default="", verbose_name="基本量纲html符号")
    basic_dimension_symbol_latex = models.CharField(
        max_length=256, null=False, blank=True, default="", verbose_name="基本量纲latex符号")
    basic_dimension_symbol_unicode = models.CharField(
        max_length=256, null=False, blank=True, default="", verbose_name="基本量纲unicode符号")
    basic_dimension_description = models.TextField(
        verbose_name='基本量纲描述', null=False, blank=True, default="")
    basic_dimension_description_en = models.TextField(
        verbose_name='基本量纲英文描述', null=False, blank=True, default="")
    basic_dimension_comment = models.TextField(
        verbose_name='基本量纲备注', null=False, blank=True, default="")
    create_person = models.ForeignKey(
        User, verbose_name="创建人", related_name="create_basicdimension", on_delete=models.DO_NOTHING, null=False)
    create_date = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    modify_person = models.ForeignKey(
        User, verbose_name="修改人", related_name="modify_basicdimension", on_delete=models.DO_NOTHING, null=False)
    modify_date = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    valid_flag = models.BooleanField(default=True, verbose_name="是否有效")

    class Meta:
        ordering = ['create_date']
        verbose_name = "BasicDimension"
        verbose_name_plural = "BasicDimensions"

        constraints = [
            models.UniqueConstraint(fields=['basic_dimension_name'], condition=models.Q(
                valid_flag=True), name='unique_basic_dimension_name'),
            models.UniqueConstraint(fields=['basic_dimension_name_en'], condition=models.Q(
                valid_flag=True) & ~models.Q(basic_dimension_name_en=""), name='unique_valid_basic_dimension_name_en'),
            models.UniqueConstraint(fields=["sei_id"], condition=models.Q(
                valid_flag=True) & ~models.Q(sei_id=""), name="unique_valid_basic_dimension_sei_id"),
        ]

    def __str__(self):
        return "{}({})".format(self.basic_dimension_name, self.basic_dimension_name_en)

    def show_full_name(self):
        if self.basic_dimension_name != "" and self.basic_dimension_name_en != "":
            return "{}({})".format(self.basic_dimension_name, self.basic_dimension_name_en)
        if self.basic_dimension_name != "":
            return self.basic_dimension_name
        if self.basic_dimension_name_en != "":
            return self.basic_dimension_name_en
        return "未命名基本量纲"

    def show_short_name(self):
        if self.basic_dimension_name != "":
            return self.basic_dimension_name
        if self.basic_dimension_name_en != "":
            return self.basic_dimension_name_en
        return "未命名基本量纲"
